/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yheriatovych.reductor.example.dialog;

import com.yheriatovych.reductor.example.ResourceTable;
import com.yheriatovych.reductor.example.util.NumCalcUtil;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * 选择弹出框
 *
 * @since 2021-05-11
 */
public class SelectedDialog extends CommonDialog implements Component.ClickedListener {
    private static final float DIALOGWIDTHVP = 220;

    private static final float DIALOGHEIGHTVP = 170;

    private static final int NUM2 = 2;

    private SelectedListener mSelectedListener;

    /**
     * 构造器
     *
     * @param context
     * @param selectedListener
     * @param offsetY 垂直位置
     */
    public SelectedDialog(Context context, SelectedListener selectedListener, float offsetY) {
        super(context);
        int dialogWidth = AttrHelper.vp2px(DIALOGWIDTHVP, context);
        int dialogHeight = AttrHelper.vp2px(DIALOGHEIGHTVP, context);
        mSelectedListener = selectedListener;
        setAutoClosable(true)
                .setAlignment(LayoutAlignment.CENTER)
                .setSize(dialogWidth, dialogHeight);
        setOffset(0,
                -(int) (NumCalcUtil.subtract(offsetY, NumCalcUtil.divide(dialogHeight, NUM2))));
        initLayout(context);
    }

    private void initLayout(Context context) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_selected, null,false);
        setContentCustomComponent(component);
        initSelectedEvent(component);
    }

    private void initSelectedEvent(Component component) {
        Component all = component.findComponentById(ResourceTable.Id_all);
        Component checked = component.findComponentById(ResourceTable.Id_checked);
        Component unchecked = component.findComponentById(ResourceTable.Id_unchecked);
        all.setClickedListener(this::onClick);
        checked.setClickedListener(this::onClick);
        unchecked.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component component) {
        switch(component.getId()) {
            case ResourceTable.Id_checked: mSelectedListener.selected(1); break;
            case ResourceTable.Id_unchecked: mSelectedListener.selected(NUM2); break;
            default: mSelectedListener.selected(0);
        }
        this.destroy();
    }
}
